<template>
  <div>
    <el-page-header @back="goBack"></el-page-header>
    <div v-show="active==1">
      <span style="margin-left: 650px;font-size: 30px">安全日志</span>
      <el-divider content-position="right"></el-divider>
      <div style="width: 1300px">
        <span style="font-size:20px;color: red">基本信息</span>
        <el-form label-width="200px" :model="securitylogForm">
          <el-row>
            <el-col :span="12">
              <el-form-item label="日期:">
                <el-col :span="11">
                  <el-date-picker
                    type="date"
                    placeholder="选择日期"
                    style="width: 450px;"
                    value-format="yyyy-MM-dd"
                    v-model="securitylogForm.updateDate"
                  ></el-date-picker>
                </el-col>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="日志编号:">
                <el-input disabled placeholder="保存后自动生成"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="项目名称:" required>
                <input type="hidden" v-model="securitylogForm.pid"/>
                <el-input v-model="securitylogForm.projectName" @focus="pid" clearable></el-input>
                <!-- <el-select v-model="securitylogForm.pid" style="width: 450px;">
                  <el-option label="--请选择--" value></el-option>
                  <el-option
                    v-for="item in projectname"
                    :key="item.id"
                    :label="item.projectName"
                    :value="item.id"
                  ></el-option>
                </el-select>-->
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="出勤人数:">
                <el-input v-model="securitylogForm.workerCount"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="温度:">
                <el-input v-model="securitylogForm.temperature"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="施工部位:" required>
                <el-input v-model="securitylogForm.workPart"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="部门:" required>
                <el-select v-model="securitylogForm.deptId" style="width: 450px;">
                  <el-option label="--请选择--" value></el-option>
                  <el-option
                    v-for="item in deptname"
                    :key="item.id"
                    :label="item.deptname"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="编写人:" required>
                <el-select v-model="securitylogForm.fillUser" style="width: 450px;">
                  <el-option label="--请选择--" value></el-option>
                  <el-option
                    v-for="item in username"
                    :key="item.id"
                    :label="item.userRealname"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-form-item label="当日施工安全情况:" required>
              <el-input type="textarea" v-model="securitylogForm.securitySituation"></el-input>
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item label="安全教育情况:">
              <el-input type="textarea" v-model="securitylogForm.securityEduation"></el-input>
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item label="安全交底:">
              <el-input type="textarea" v-model="securitylogForm.securityDisclosure"></el-input>
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item label="验收情况记录:">
              <el-input type="textarea" v-model="securitylogForm.acceptRecord"></el-input>
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item label="安全检查情况:">
              <el-input type="textarea" v-model="securitylogForm.securityCheck"></el-input>
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item label="安全会议记录:">
              <el-input type="textarea" v-model="securitylogForm.securityMeetingRecord"></el-input>
            </el-form-item>
          </el-row>
          <multi-file-upload
            style="font-size:20px;color: red"
            title="附件"
            action="http://localhost/uploads"
            :params="{folder: 'securitylog'}"
            :headers="{token: token}"
            :show-button="!checkRoute"
            @uploadSuccess="handleUploadSuccess"
            :parent-file-list="fileList"
          ></multi-file-upload>
          <el-form-item>
            <el-button
              type="primary"
              style="margin-left: 1000px"
              @click="addsecuritylog(securitylogForm)"
            >确认提交</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <!--所属项目子组件-->
    <el-dialog :visible.sync="dialogVisible" width="50%">
      <project-info-list v-if="dialogVisible" @row="project"></project-info-list>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
import { DateUtil } from "../../../model/common/DateUtil";
import ProjectInfoList from '../../common/projectInfo/ProjectInfoList.vue';
export default {
  components: { ProjectInfoList },
  data() {
    return {
      dialogVisible: false, //隐藏子组件
      active: 1,
      fileList: [],
      securitylogForm: {
        updateDate: DateUtil.today()
      },
      // securitylog: [],
      securitylog: {
        id: "",
        date: DateUtil.today(),
        planEditor: sessionStorage.getItem("realName")
      },
      projectname: [],
      deptname: [],
      username: [],
      projectId: "" //所属项目id
    };
  },
  methods: {
    //开启隐藏项目子组件
    pid() {
      this.dialogVisible = true;
    },

    //响应项目组件
    project(row) {
      this.securitylogForm.pid = row.id;
      this.securitylogForm.projectName = row.projectName;
     this.projectId = row.id; //获取所属项目id
      this.dialogVisible = false;
    },
    //查询部门名称并渲染到下拉框
    DeptNameList() {
      axios.get(`http://localhost/SecurityLog/DeptName`).then(response => {
        this.deptname = response.data.data;
      });
    },
    //查询项目名称并渲染到下拉框
    ProjectNameList() {
      axios.get(`http://localhost/SecurityLog/ProjectName`).then(response => {
        this.projectname = response.data.data;
      });
    },
    //查询填报人名称并渲染到下拉框
    UserNameList() {
      axios.get(`http://localhost/SecurityLog/UserName`).then(response => {
        this.username = response.data.data;
      });
    },
    //查询安全日志
    SecurityLogList() {
      axios
        .get(`http://localhost/SecurityLog`, { params: this.securitylogfrom })
        .then(response => {
          this.securitylog = response.data.data;
        });
    },
    goBack() {
      history.back();
    },
    handleUploadSuccess(ids) {
      this.securitylogForm.other = ids;
      console.log(this.securitylogForm.other);
    },
    //添加安全日志信息
    addsecuritylog() {
      if (
        this.securitylogForm.pid == "" ||
        this.securitylogForm.pid == undefined
      ) {
        this.$message.error("项目名称不能为空");
      } else {
        if (
          this.securitylogForm.workPart == "" ||
          this.securitylogForm.workPart == undefined
        ) {
          this.$message.error("施工部位不能为空");
        } else {
          if (
            this.securitylogForm.deptId == "" ||
            this.securitylogForm.deptId == undefined
          ) {
            this.$message.error("部门不能为空");
          } else {
            if (
              this.securitylogForm.fillUser == "" ||
              this.securitylogForm.fillUser == undefined
            ) {
              this.$message.error("编写人不能为空");
            } else {
              if (
                this.securitylogForm.securitySituation == "" ||
                this.securitylogForm.securitySituation == undefined
              ) {
                this.$message.error("当日施工安全情况不能为空");
              } else {
                axios
                  .post("http://localhost/SecurityLog", this.securitylogForm)
                  .then(() => {
                    this.$message("添加成功");
                    this.$router.push({ path: "/SecurityLogList" });
                    this.securitylogForm = {};
                  });
              }
            }
          }
        }
      }
    }
  },

  created() {
    this.SecurityLogList();
    this.ProjectNameList();
    this.DeptNameList();
    this.UserNameList();
  },
  computed: {
    token: {
      get() {
        return this.$store.state.token;
      }
    },
    checkRoute() {
      return Object.keys(this.$route.params).length != 0;
    }
  }
};
</script>

<style>
</style>